/****************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "DevMiiTask.hpp"
#include "../ManageTools.h"
#include "../../Controls/Application.h"
#include "../../Menu/Explorer.h"
#include "../../Miis/MiiInstaller.h"
#include "../../FileOperations/fileops.h"

DevMiiTask::DevMiiTask(short Type)
	: type(Type)
	, canceled(false)
	, window(NULL)
{
	ThreadedTaskHandler::Instance()->AddTask(this);
}

void DevMiiTask::OnCancelClick()
{
	canceled = true;
}

void DevMiiTask::Execute(void)
{
	if(type == MOVE_TO_DEV)
		window = new ManageProgressWindow(tr("Moving..."));
	else if(type == COPY_TO_DEV)
		window = new ManageProgressWindow(tr("Copying..."));
	else if(type == DELETE_FROM_DEV)
		window = new ManageProgressWindow(tr("Deleting..."));
	else if(type == INSTALL_TO_NAND || type == INSTALL_TO_ENAND)
		window = new ManageProgressWindow(tr("Installing..."));
	window->Canceled.connect(this, &DevMiiTask::OnCancelClick);
	
	std::string srcPath = CurrentBrowser()->GetCurrentSelectedFilepath();
	
	int result = -1;
	if(type == DELETE_FROM_DEV)
	{
		if(RemoveFile(srcPath))
			result = 1;
	}
	else if(type == COPY_TO_DEV || type == MOVE_TO_DEV)
	{
		std::string destPath = Settings.TmpPath;
		if(destPath[destPath.size()-1] != '/')
			destPath += "/";
		destPath += CurrentBrowser()->GetItemName(CurrentBrowser()->GetSelIndex());
		
		if(type == COPY_TO_DEV)
			result = CopyFile(srcPath.c_str(), destPath.c_str());
		else
			result = MoveFile(srcPath.c_str(), destPath.c_str());
	}
	else if(type == INSTALL_TO_NAND || type == INSTALL_TO_ENAND)
	{
		int position = -1; //! position of the mii in (e)Nand file
		
		Browser * browser = NULL;
		MiiBrowser * miibrowser = NULL;
		EMiiBrowser * emiibrowser = NULL;
		if(type == INSTALL_TO_NAND)
		{
			miibrowser = new MiiBrowser();
			browser = miibrowser;
		}
		else
		{
			emiibrowser = new EMiiBrowser();
			emiibrowser->GetList(Settings.TmpPath);
			browser = emiibrowser;
		}
		
		std::wstring srcName = CurrentBrowser()->GetCurrentDisplayname();
		
		for(int n = 0; n < browser->GetEntrieCount(); n++)
		{
			if(!browser->GetMiiName(n).compare(srcName))
			{
				window->Hide();
				
				ManageButtons * overWriteButtons = new ManageButtons(tr( "is already installed." ), 0, tr( "OverWrite" ), tr( "Duplicate" ), tr( "Cancel" ));
				overWriteButtons->SetEffect(EFFECT_FADE, 20);
				overWriteButtons->SetAlignment(ALIGN_CENTER | ALIGN_MIDDLE);
				overWriteButtons->SetPosition(0, 80);
				Application::Instance()->Append(overWriteButtons);
				Application::Instance()->SetUpdateOnly(overWriteButtons);
				
				int choice = 0;
				while (!(choice = overWriteButtons->GetChoice()))
				{	
					if(Application::isClosing())
					{
						Application::Instance()->UnsetUpdateOnly(overWriteButtons);
						Application::Instance()->Remove(overWriteButtons);
						delete overWriteButtons;
						delete browser;
						delete window;
						return;
					}
					else
						usleep(50);
				}
				
				overWriteButtons->SetEffect(EFFECT_FADE, -20);
				while (overWriteButtons->IsAnimated())
					usleep(50);
				Application::Instance()->UnsetUpdateOnly(overWriteButtons);
				Application::Instance()->Remove(overWriteButtons);
				delete overWriteButtons;
				
				if(choice != 3)
				{
					window->Show();
					
					if(choice == 1)
						position = browser->GetMiiPosition(n);
					else
						position = MiiTools::FindFirstFree(browser);
					
					n = browser->GetEntrieCount();
				}
				else
				{
					canceled = true;
					n = browser->GetEntrieCount();
				}
			}
		}
		if(!canceled && (position < 0))
			position = MiiTools::FindFirstFree(browser);
		
		if(type == INSTALL_TO_NAND)
			delete miibrowser;
		else
			delete emiibrowser;
		browser = NULL;
		
		if(!canceled)
		{
			if(type == INSTALL_TO_NAND)
				result = Mii::InstallNand(srcPath, position);
			else
				result = Mii::InstallEmu(srcPath, position, ManageTools::GetENandNumber(Settings.TmpPath));
		}
	}
	
	delete window;
	
	if(canceled)
		result = 0;
	
	End(this, result);
}
